using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Web.Security;

using MvcCms.Data;
using MvcCms.Data.Entities;
using MvcCms.Service.Entities;
using MvcCms.Service;
using MvcCms.Service.Validation;
using MvcCms.Web.Code;
using MvcCms.Web.ViewModels;
using MvcCms.Service.Cache;
using MvcCms.Service.Logging;
using Microsoft.Practices.ServiceLocation;
using MvcCms.Service.Code.Email;

namespace MvcCms.Web.Controllers
{
    public class AccountAjaxController : MvcCms.Service.Code.BaseClasses.MvcCmsControllerBase
    {
        #region Constructors

        private readonly IAccountService _accountService;
        private readonly ICmsService _cmsService;
        private readonly IMiscService _miscService;
        private readonly ISearchService _searchService;
        private readonly ICacheService _cacheService;
        private readonly IPortalService _portalService;
        public AccountAjaxController(ICmsService cmsService, IMiscService miscService, IAccountService accountService, ISearchService searchService, ICacheService cacheService, IPortalService portalService)
        {
            _cmsService = cmsService;
            _accountService = accountService;
            _searchService = searchService;
            _miscService = miscService;
            _cacheService = cacheService;
            _portalService = portalService;


        }

        #endregion


        [Authorize(Roles = "Admin, Editor")]
        public JsonResult AddUserToRole(Guid roleId, Guid userId)
        {
            try
            {
                var role = _accountService.GetRole(roleId);
                var user = _accountService.GetUser(userId);
                Roles.AddUserToRole(user.UserName, role.RoleName);
                return Json(new { result = "success" });
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(ex.Message, ex);
                return Json(new { error = ex.Message });
            }
        }
        [Authorize(Roles = "Admin, Editor")]
        public JsonResult RemoveUserFromRole(Guid roleId, Guid userId)
        {
            try
            {
                var role = _accountService.GetRole(roleId);
                var user = _accountService.GetUser(userId);
                Roles.RemoveUserFromRole(user.UserName, role.RoleName);
                return Json(new { result = "success" });
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(ex.Message, ex);
                return Json(new { error = ex.Message });
            }
        }
    }
}